package com.spc.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.spc.dao.Menudao;
import com.spc.dao.RoleDao;
import com.spc.entity.PageResult;
import com.spc.entity.QueryPageBean;
import com.spc.pojo.Menu;
import com.spc.pojo.Role;
import com.spc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private Menudao menudao;

    //添加角色
    public void addRole(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //先添加角色
        roleDao.addRole(role);
        //添加关联表
        Map<String, Object> map = new HashMap<>();
        //获取新插入角色的id,放入map
        if (permissionIds!=null && permissionIds.length>0) {
            map.put("roleId", role.getId());
            map.put("PermissionId", permissionIds);
            this.addRelated(map);
        }
        //添加角色管理菜单表
        //根据角色id查询到所有子菜单
        List<Integer> menus = new ArrayList<>();
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                menus.add(menuId);
                List<Menu> byidzi = menudao.findByidzi(menuId);
                if (byidzi != null) {
                    for (Menu menu : byidzi) {
                        menus.add(menu.getId());
                    }
                }
            }
            Map<String, Object> HashMap = new HashMap<>();
            HashMap.put("roleId", role.getId());
            HashMap.put("menuId", menus);
            roleDao.addRelatedMenu(HashMap);
        }
    }

    //查寻所有角色
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Role> roles = roleDao.queryString(queryPageBean.getQueryString());
        return new PageResult(roles.getTotal(), roles.getResult());
    }

    //根据id查询角色
    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    //根据角色ID查询关联的所有权限
    @Override
    public List<Integer> findIds(Integer id) {

        return roleDao.findIds(id);
    }

    //修改编辑角色
    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //修改角色表
        roleDao.edit(role);
        //删除权限关联表
        roleDao.delRelated(role.getId());
        //删除菜单关联表
        roleDao.delRelatedm(role.getId());
        if (permissionIds != null && permissionIds.length > 0) {
            //添加权限关联表
            Map<String, Object> map = new HashMap<>();
            //获取新插入角色的id,放入map
            map.put("roleId", role.getId());
            map.put("PermissionId", permissionIds);
            this.addRelated(map);
        }
        //添加菜单关联表
        //根据角色id查询到所有子菜单
        List<Integer> menus = new ArrayList<>();
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                menus.add(menuId);
                List<Menu> byidzi = menudao.findByidzi(menuId);
                if (byidzi != null) {
                    for (Menu menu : byidzi) {
                        menus.add(menu.getId());
                    }
                }
            }
            Map<String, Object> HashMap = new HashMap<>();
            HashMap.put("roleId", role.getId());
            HashMap.put("menuId", menus);
            roleDao.addRelatedMenu(HashMap);
        }
    }

    //删除角色
    @Override
    public void delete(Integer id) {
        //查询是否关联
        Integer integer = roleDao.findIds1(id);
        if (integer > 0) {
            //删除关联表
            roleDao.delRelated(id);

        }
        //查询是否关联用户表
        Integer integer1 = roleDao.findByrid(id);
        if (integer1 > 0) {
            roleDao.delRelateduser(id);
        }

        //查询是否关联菜单表
        Integer[] findmenus = roleDao.findmenus(id);
        if (findmenus.length > 0 && findmenus != null) {
            roleDao.delRelatedm(id);
        }
        //删除角色
        roleDao.delete(id);
    }

    //根据角色id查询包含的所有父菜单权限
    @Override
    public List<Integer> findmenus(Integer id) {
        //查询角色包含的所有菜单
        Integer[] integers = roleDao.findmenus(id);
        List<Integer> list = new ArrayList<>();
        //遍历菜单取出所有父菜单
        List<Menu> findbyidfu = menudao.findbyidfu(integers);
        for (Menu integer : findbyidfu) {
            if (integer != null) {
                //将父id放入集合
                list.add(integer.getId());
            }

        }
        return list;
    }


        //抽取关联关系,代码重复
        public void addRelated (Map map){
            roleDao.addRelated(map);//调用dao方法传入map,将数据放入关联表
        }
    }

